import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Pet from '../views/Pet.vue'
import Index from '../views/Index.vue'
import User from '../views/User.vue'
import Chart from '../views/Chart.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/home',
    name:'Home',
    component:Home
  },
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/pet',
    name: 'Pet',
    component: Pet
  },
  {
    path: '/index',
    name: 'Index',
    component: Index,
    redirect:'/user', // 重定向
    children:[{  // 子路由
      path:'/user',
      component:User
    },{  
      path:'/chart',
      component:Chart
    }]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫,to要去的页面，from，从哪里来，next，下一个操作
router.beforeEach((to,from,next) => {
  if(to.path === '/'){
    return next()
  }

  // 有token就放行，没有token就去登陆页面获取token
  const myToken = sessionStorage.getItem('jhToken');
  if(myToken){
    return next()
  }else{
    // 不存在去登陆页面
    next('')
  }
  // console.log(to);  
  // console.log(from);
  // next();
})

export default router
